package com.nanjia.boot.product.spec.value.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.common.enums.StatusEnum;
import com.nanjia.boot.core.security.util.SecurityUtils;
import com.nanjia.boot.product.spec.value.converter.SpecValueConverter;
import com.nanjia.boot.product.spec.value.mapper.SpecValueMapper;
import com.nanjia.boot.product.spec.value.model.entity.SpecValue;
import com.nanjia.boot.product.spec.value.model.form.SpecValueForm;
import com.nanjia.boot.product.spec.value.model.query.SpecValueQuery;
import com.nanjia.boot.product.spec.value.model.vo.SpecValueVO;
import com.nanjia.boot.product.spec.value.service.SpecValueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 规格值服务实现类
 *
 * @author 周洋
 * @since 2025-07-09 17:16
 */
@Service
@RequiredArgsConstructor
public class SpecValueServiceImpl extends ServiceImpl<SpecValueMapper, SpecValue> implements SpecValueService {

    private final SpecValueConverter specValueConverter;

    /**
    * 获取规格值分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SpecValueVO>} 规格值分页列表
    */
    @Override
    public IPage<SpecValueVO> getSpecValuePage(SpecValueQuery queryParams) {
        Page<SpecValueVO> pageVO = this.baseMapper.getSpecValuePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取规格值表单数据
     *
     * @param id 规格值ID
     * @return 规格值表单数据
     */
    @Override
    public SpecValueForm getSpecValueFormData(Long id) {
        SpecValue entity = this.getById(id);
        return specValueConverter.toForm(entity);
    }
    
    /**
     * 新增规格值
     *
     * @param formData 规格值表单对象
     * @param ipAddr
     * @return 是否新增成功
     */
    @Override
    public boolean saveSpecValue(SpecValueForm formData, String ipAddr) {
        SpecValue entity = specValueConverter.toEntity(formData);

        entity.setCreateBy(SecurityUtils.getUserStrId());
        entity.setCreateIp(ipAddr);
        entity.setCreateTime(LocalDateTime.now());
        entity.setStatus(StatusEnum.ENABLE.getValue());
        entity.setUpdateTime(LocalDateTime.now());
        return this.save(entity);
    }
    
    /**
     * 更新规格值
     *
     * @param id       规格值ID
     * @param formData 规格值表单对象
     * @param ipAddr
     * @return 是否修改成功
     */
    @Override
    public boolean updateSpecValue(Long id, SpecValueForm formData, String ipAddr) {
        SpecValue entity = specValueConverter.toEntity(formData);
        entity.setId(id);
        entity.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
        entity.setUpdateIp(ipAddr);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }
    
    /**
     * 删除规格值
     *
     * @param ids 规格值ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSpecValues(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的规格值数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        List<SpecValue> list = new ArrayList<>();
        for (Long l : idList) {
            SpecValue specValue = new SpecValue();
            specValue.setId(l);
            specValue.setUpdateTime(LocalDateTime.now());
            specValue.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
            specValue.setStatus(StatusEnum.DELETE.getValue());
            list.add(specValue);
        }
        return this.updateBatchById(list, idList.size());
    }

}
